﻿using Learun.Util;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-03-07 13:46
    /// 描 述：工程数据属性表
    /// </summary>
    public class ec_enginedata_propertyBLL : ec_enginedata_propertyIBLL
    {
        private ec_enginedata_propertyService ec_enginedata_propertyService = new ec_enginedata_propertyService();

        #region 获取数据

        /// <summary>
        /// 获取工程数据属性表数据
        /// <summary>
        /// <param name="EngineDataID">工程数据ID</param>
        /// <param name="ProjectId">项目ID</param>
        /// <returns></returns>
        public IEnumerable<ec_enginedata_propertyEntity> GetEnginePropById(string ProjectId, string EngineDataID, string CaseID = "")
        {
            try
            {
                return ec_enginedata_propertyService.GetTagPropById(ProjectId, EngineDataID,CaseID);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }



        /// <summary>
        /// 获取工程数据属性表数据
        /// <summary>
        /// <param name="ProjectId">项目ID</param>
        /// <param name="EngineDataIds">工程数据ID 集合</param>
        /// <returns></returns>
        public IEnumerable<ec_enginedata_propertyEntity> GetEnginePropByList(string ProjectId, List<string> listEngineDataID, string CaseID = "")
        {
            try
            {
                return ec_enginedata_propertyService.GetEnginePropByList(ProjectId, listEngineDataID, CaseID);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

        #region 提交数据
        public void InsertTagProp(string ProjId,string TagID,ec_enginedata_propertyEntity entity)
        {
            ec_enginedata_propertyService.InsertTagProp(ProjId, TagID, entity);
        }

        /// <summary>
        /// 保存工程数据属性表数据（新增）
        /// <param name="engineDataID">工程数据ID</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEngineProp(string ProjectId, List<ec_enginedata_propertyEntity> entityList)
        {
            try
            {
                //需要新增的工程数据属性数据
                List<ec_enginedata_propertyEntity> engineDataPropInsert = new List<ec_enginedata_propertyEntity>();
                //需要更新的工程数据属性数据，并且需要插入工程数据属性历史表
                List<ec_enginedata_propertyEntity> engineDataPropUpdate = new List<ec_enginedata_propertyEntity>();
                List<ec_enginedata_propertyhisEntity> engineDataPropHis = new List<ec_enginedata_propertyhisEntity>();
                string curUserID = "";
                if (entityList != null && entityList.Count > 0)
                {
                    UserInfo userInfo = LoginUserInfo.Get();
                    curUserID = userInfo.userId;
                    var allEngineProp = GetEnginePropById(ProjectId, entityList[0]?.EngineDataID).ToList();
                    foreach (var item in entityList)
                    {
                        if (string.IsNullOrEmpty(item.EngineDataPropertyID) || item.EngineDataPropertyID == "null" )
                        {//新增属性时需要设置工程数据属性表的创建人、创建时间
                            if (!string.IsNullOrEmpty(item.PropertyValue))
                            {//新增时没有属性值的不需要保存
                                item.Create();
                                engineDataPropInsert.Add(item);
                            }
                        }
                        else
                        {
                            //更新时需要判断属性值是否有修改
                            var tempModel = allEngineProp.Find(x => x.PropertyName == item.PropertyName);
                            if (tempModel?.PropertyValue != item.PropertyValue)
                            {//更新属性时需要设置工程数据属性表的更新人、更新时间，同时，插入工程数据属性历史表。
                                if (string.IsNullOrWhiteSpace(tempModel?.CreateUserID) && tempModel?.CreateTime == null) 
                                {
                                    item.CreateTime = Time.MySqlTime;
                                    item.CreateUserID = curUserID;
                                }
                                item.Modify(item.EngineDataPropertyID);
                                engineDataPropUpdate.Add(item);
                                ec_enginedata_propertyhisEntity modelHis = new ec_enginedata_propertyhisEntity();
                                modelHis.Create();
                                modelHis.EngineDataID = item.EngineDataID;
                                modelHis.PropertyName = item.PropertyName;
                                modelHis.PropertyValue = tempModel?.PropertyValue;
                                engineDataPropHis.Add(modelHis);
                            }
                        }
                    }
                }
                ec_enginedata_propertyService.SaveEngineProp(ProjectId, engineDataPropInsert, engineDataPropUpdate, engineDataPropHis);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 更新默认工程数据属性
        /// <summary>
        /// <returns></returns>
        public void UpdateEnginePropByDefault(string ProjId)
        {
            try
            {
                ec_enginedata_propertyService.UpdateEnginePropByDefault(ProjId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 插入默认工程数据属性
        /// <summary>
        /// <returns></returns>
        public void InsertEnginePropByDefault(string ProjId)
        {
            try
            {
                ec_enginedata_propertyService.InsertEnginePropByDefault(ProjId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion

    }
}
